{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "|WARNING|2018-11-06 15:16:56,651|Default account set to 648329, to change use set_default_account().\n"
     ]
    }
   ],
   "source": [
    "import fxcmpy\n",
    "import time\n",
    "import pandas as pd\n",
    "import json\n",
    "import datetime as dt\n",
    "\n",
    "api = fxcmpy.fxcmpy(access_token='9ecadb8c9e41afbf5ad00c827e9cd547eff22110') #log_level = 'error'\n",
    "\n",
    "# note that I'm using the token of my demo account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "|WARNING|2018-11-06 15:16:57,157|Got a insert event for open positions: {'t': 1, 'ratePrecision': 5, 'tradeId': '114708046', 'accountName': '00648329', 'accountId': '648329', 'roll': 0, 'com': 0, 'open': 1.14145, 'valueDate': '', 'grossPL': -0.0993, 'close': 1.14132, 'visiblePL': -1.3, 'isDisabled': False, 'currency': 'EUR/USD', 'isBuy': True, 'amountK': 1, 'currencyPoint': 0.07638, 'time': '11062018201656', 'usedMargin': 10, 'stop': 0, 'stopMove': 0, 'limit': 0, 'action': 'I'}.\n",
      "|WARNING|2018-11-06 15:16:57,167|Got a delete event for orders: {'t': 3, 'orderId': '196972561', 'action': 'D'}.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "entry price  1.14146\n",
      "1st pos opened\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "|WARNING|2018-11-06 15:17:48,637|Got a delete event for orders: {'t': 3, 'orderId': '196972562', 'action': 'D'}.\n"
     ]
    }
   ],
   "source": [
    "A=2.5e-4\n",
    "\n",
    "api.open_trade('EUR/USD', amount=1, is_buy=True, order_type='AtMarket', time_in_force='GTC')\n",
    "\n",
    "while api.get_open_trade_ids() == []:\n",
    "    time.sleep(0.2)\n",
    "\n",
    "trade_ID = api.get_open_trade_ids()[-1]\n",
    "entry_price = api.get_open_positions(kind='dataframe')['open'].iloc[-1]\n",
    "\n",
    "TP = round(entry_price + A, 5)\n",
    "SL = round(entry_price - 2*A, 5)\n",
    "print('entry price ', entry_price)\n",
    "    \n",
    "\n",
    "\n",
    "    \n",
    "order1 = api.create_entry_order(symbol='EUR/USD', is_buy=False,\n",
    "                       amount=1, limit=entry_price-2*A,\n",
    "                       is_in_pips = False,\n",
    "                       rate=entry_price-A, stop=entry_price, trailing_step=None,\n",
    "                       trailing_stop_step=None,\n",
    "                       time_in_force='GTC')\n",
    "print('1st pos opened')\n",
    "\n",
    "while order1.get_status()=='Waiting':\n",
    "    time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2nd pos opened\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "|WARNING|2018-11-06 15:20:20,463|Got a insert event for open positions: {'t': 1, 'ratePrecision': 5, 'tradeId': '114708053', 'accountName': '00648329', 'accountId': '648329', 'roll': 0, 'com': 0, 'open': 1.14146, 'valueDate': '', 'grossPL': -0.10694, 'close': 1.14132, 'visiblePL': -1.4, 'isDisabled': False, 'currency': 'EUR/USD', 'isBuy': True, 'amountK': 1, 'currencyPoint': 0.07638, 'time': '11062018202019', 'usedMargin': 10, 'stop': 1.14121, 'stopMove': 0, 'limit': 1.14171, 'action': 'I'}.\n",
      "|WARNING|2018-11-06 15:20:20,484|Got a delete event for orders: {'t': 3, 'orderId': '196972568', 'action': 'D'}.\n",
      "|WARNING|2018-11-06 15:51:22,178|Got a insert event for closed positions: {'t': 2, 'ratePrecision': 5, 'tradeId': '114708053', 'accountName': '00648329', 'roll': 0, 'com': 0, 'open': 1.14146, 'valueDate': '', 'grossPL': 0.2, 'close': 1.14172, 'visiblePL': 2.6, 'currency': 'EUR/USD', 'isBuy': True, 'amountK': 1, 'currencyPoint': 0.07638, 'openTime': '11062018202019', 'closeTime': '11062018205121', 'action': 'I'}.\n",
      "|WARNING|2018-11-06 15:51:22,180|Got a delete event for open posi: {'t': 1, 'tradeId': '114708053', 'action': 'D'}\n",
      "|WARNING|2018-11-06 15:51:22,195|Got a delete event for orders: {'t': 3, 'orderId': '196972794', 'action': 'D'}.\n",
      "|WARNING|2018-11-06 15:51:22,197|Got a delete event for orders: {'t': 3, 'orderId': '196972793', 'action': 'D'}.\n"
     ]
    }
   ],
   "source": [
    "order2 = api.create_entry_order(symbol='EUR/USD', is_buy=True,\n",
    "                       amount=1, limit=1.14171,\n",
    "                       is_in_pips = False,\n",
    "                       rate=1.14146, stop=1.14121, trailing_step=None,\n",
    "                       trailing_stop_step=None,\n",
    "                       time_in_force='GTC')                                \n",
    "print('2nd pos opened')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
